﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Logic.POI;

namespace HarmoniousVigor
{
    public partial class Shaman
    {
        public bool elementalCombat()
        {
            if (Battlegrounds.IsInsideBattleground)
            {
                if (elementalPVPCombat())
                    return true;
            }
            else if (characterPlayer.IsInInstance)
            {
                if (elementalDungeonCombat())
                    return true;
            }
            else
            {
                if (elementalQuestingCombat())
                    return true;
            }

            return false;
        }
        public bool elementalHeal()
        {
            if (Battlegrounds.IsInsideBattleground)
            {
                if (elementalPVPHeal())
                    return true;
            }
            else if (characterPlayer.IsInInstance)
            {
                if (elementalDungeonHeal())
                    return true;
            }
            else
            {
                if (elementalQuestingHeal())
                    return true;
            }

            return false;
        }
        public bool elementalBuff()
        {
            if (Battlegrounds.IsInsideBattleground)
            {
                if (elementalPVPBuff())
                    return true;
            }
            else if (characterPlayer.IsInInstance)
            {
                if (elementalDungeonBuff())
                    return true;
            }
            else
            {
                if (elementalQuestingBuff())
                    return true;
            }

            return false;
        }
        public bool elementalPullSpell()
        {
            if (Battlegrounds.IsInsideBattleground)
            {
                if (elementalPVPPullSpell())
                    return true;
            }
            else if (characterPlayer.IsInInstance)
            {
                if (elementalDungeonPullSpell())
                    return true;
            }
            else
            {
                if (elementalQuestingPullSpell())
                    return true;
            }

            return false;
        }


        public bool elementalCombatBehavior()
        {
            if (targetRange < 8 || characterPlayer.ManaPercent < 60)
            {
                if (Utils.CastMe("Thunderstorm"))
                    return true;
            }

            if (targetRange < 45)
            {
                if (characterPlayer.HasAura("Fulmination") && characterPlayer.Buffs["Lightning Shield"].StackCount == 9)
                {
                    if (Utils.CastTarget("Earth Shock", characterPlayer.CurrentTarget))
                        return true;
                }
            }

            if (targetRange < 45)
            {
                if (!(characterPlayer.CurrentTarget.HasAura("Flame Shock")
                    && characterPlayer.Debuffs["Flame Shock"].StackCount > 0
                    && characterPlayer.Debuffs["Flame Shock"].CreatorGuid == characterPlayer.Guid))
                {
                    if (Utils.CastTarget("Flame Shock", characterPlayer.CurrentTarget))
                        return true;
                }
            }


            if (targetRange < 40)
            {
                if (SpellManager.CanCast("Lava Burst"))
                {
                    if (characterPlayer.IsMoving)
                    {
                        Navigator.PlayerMover.MoveStop();
                        StyxWoW.SleepForLagDuration();
                    }
                    if (Utils.CastTarget("Lava Burst", characterPlayer.CurrentTarget))
                        return true;
                }
            }

            if (targetRange < 40)
            {
                if (Utils.WillChainEnemyDistanceFrom(12, characterPlayer.CurrentTarget) > 2 && SpellManager.CanCast("Chain Lightning"))
                {
                    if (characterPlayer.IsMoving)
                    {
                        Navigator.PlayerMover.MoveStop();
                        StyxWoW.SleepForLagDuration();
                    }
                    if (Utils.CastTarget("Chain Lightning", characterPlayer.CurrentTarget))
                        return true;
                }
            }

            if (targetRange < 40)
            {
                if (SpellManager.CanCast("Lightning Bolt"))
                {
                    if (characterPlayer.IsMoving && !lightningBoltWhileMoving)
                    {
                        Navigator.PlayerMover.MoveStop();
                        StyxWoW.SleepForLagDuration();
                    }
                    if (Utils.CastTarget("Lightning Bolt", characterPlayer.CurrentTarget))
                        return true;
                }
            }

            return false;
        }
        public bool elementalBuffBehavior()
        {
            if (!characterPlayer.Inventory.Equipped.MainHand.TemporaryEnchantment.Name.Equals("Flametongue"))
            {
                if (Utils.BuffMe("Flametongue Weapon"))
                    return true;
            }

            if (!characterPlayer.HasAura("Lightning Shield"))
            {
                if (Utils.BuffMe("Lightning Shield"))
                    return true;
            }

            if (characterPlayer.Combat)
            {
                if (!characterPlayer.HasAura("Time Warp") && !characterPlayer.HasAura("Bloodlust"))
                {
                    if (Utils.CastMe("Bloodlust"))
                        return true;
                }
            }

            return false;
        }
        public bool elementalInterruptBehavior()
        {
            if (targetRange < 30)
            {
                if ((characterPlayer.CurrentTarget.IsCasting && characterPlayer.CurrentTarget.CanInterruptCurrentSpellCast
                    && characterPlayer.CurrentTarget.CurrentCastTimeLeft < new TimeSpan(0, 0, 0, 0, 2000))
                    || (characterPlayer.CurrentTarget.ThreatInfo.RawPercent > 90 && characterPlayer.IsInInstance))
                {
                    if (Utils.CastTarget("Wind Shear", characterPlayer.CurrentTarget))
                        return true;
                }
            }

            return false;
        }
        public bool elementalCrowdControlBehavior()
        {
            WoWUnit hexTarget = ObjectManager.GetObjectsOfType<WoWUnit>(false, false).FirstOrDefault(unit => unit != characterPlayer.CurrentTarget
                      && !unit.IsFriendly && unit.Distance2D <= 30 && unit.Combat && unit.IsAlive && unit.Aggro
                      && (unit.CreatureType == WoWCreatureType.Humanoid || unit.CreatureType == WoWCreatureType.Beast));
            if (hexTarget != null)
            {
                if (Utils.CastTarget("Hex", hexTarget))
                {
                    HexTarget = hexTarget;
                    return true;
                }
            }
            return false;
        }
        public bool elementalRegularBehavior()
        {
            if (characterPlayer.CurrentTarget == null || characterPlayer.Mounted)
                return true;
            rangedMovement();
            faceTarget();
            AutoAttack();
            return false;
        }
        public void elementalHexCheckBehavior()
        {
            if (characterPlayer.CurrentTarget == null)
            {
                WoWUnit optionalTarget = ObjectManager.GetObjectsOfType<WoWUnit>(false, false).FirstOrDefault(unit => !unit.IsFriendly
                    && unit.Distance2D <= 40 && unit.Combat && unit.IsAlive && unit.Aggro && unit != HexTarget);
                if (optionalTarget == null)
                {
                    if (HexTarget != null)
                    {
                        if (HexTarget.Combat && HexTarget.IsAlive && HexTarget.Aggro)
                        {
                            Utils.Log("Cannot find another target, targetting Hexed target");
                            HexTarget.Target();
                        }
                        else
                        {
                            Utils.Log("Hexed target is not valid");
                            HexTarget = null;
                        }
                    }
                }
            }
        }
        public bool elementalCooldownBehavior()
        {
            if (characterPlayer.Combat)
            {
                if (Utils.BuffMe("Elemntal Mastery"))
                    return true;
            }

            return false;
        }
        public bool elementalTotemBehavior()
        {
            return false;
        }
        public bool elementalThreatDropBehavior()
        {
            if (characterPlayer.ThreatInfo.RawPercent > 90)
            {
                if (Utils.CastTarget("Wind Shear", characterPlayer.CurrentTarget))
                    return true;
            }
            return false;
        }
    }
}
